# Maintainer: Caleb Maclennan <caleb@alerque.com>
# Contributor: Jesús Espino <jespinog@gmail.com>

pkgname=mmctl
pkgver=7.10.0
pkgrel=1
pkgdesc='A remote CLI tool for Mattermost'
arch=(loong64 x86_64)
url="https://github.com/mattermost/$pkgname"
license=(Apache)
depends=(gcc-libs)
makedepends=(go
             git)
_archive="$pkgname-$pkgver"
source=("$url/archive/v$pkgver/$_archive.tar.gz")
sha256sums=('6694082e469f063769e0d8bf86db23d909006004e89ee0d26e3dd833a57fd12a')

prepare() {
	cd "$_archive"
	go mod vendor
}

build() {
	local _commit=$(zcat ${source[0]##*/} | git get-tar-commit-id)
	cd "$_archive"
	export CGO_LDFLAGS="$LDFLAGS"
	local _config=${url##https://}/commands
	go build \
		-trimpath \
		-buildmode=pie \
		-mod=readonly \
		-modcacherw \
		-ldflags "-linkmode external -X \"$_config.gitCommit=$_commit\" -X \"$_config.gitTreeState=clean\" -X \"$_config.buildDate=$(date --utc --date="@${SOURCE_DATE_EPOCH:-$(date +%s)}" +"%Y-%m-%d %H:%M:%S")\"" \
		.
}

package() {
	cd "$_archive"
	install -Dm0755 -t "$pkgdir/usr/bin/" "$pkgname"
	install -Dm0644 -t "$pkgdir/usr/share/licenses/$pkgname/" LICENSE.txt
}
